#include "apue.h"
#include <netdb.h>
#include <errno.h>
#include <sys/socket.h>
#include <arpa/inet.h>

#define BUFLEN 128

extern int connect_retry(int, int, int, const struct sockaddr *, socklen_t);

void print_pscount(int sockfd) {
    int n;
    char buf[BUFLEN];
    printf("pscount: ");
    while ((n= recv(sockfd, buf, BUFLEN, 0)) > 0)
        printf("%s", buf);
    if (n < 0)
        err_sys("recv, error");
}

int main(int argc, char *argv[]) {
    struct sockaddr_in addr;
    struct in_addr ip_addr;
    int sockfd;
    addr.sin_family= AF_INET;
    addr.sin_port= htons(8080);
    if (htonl(inet_pton(AF_INET, "127.0.0.1", &ip_addr)) < 0)
        err_sys("htonl error");
    addr.sin_addr.s_addr= ip_addr.s_addr;

    if ((sockfd= connect_retry(AF_INET, SOCK_STREAM, 0, (struct sockaddr *)&addr, sizeof(addr))) < 0)
        err_sys("connect_retry error");
        
    print_pscount(sockfd);
    exit(0);
}
